﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Text;
using VPLink2013.Framework.Constants;
using VPLink2013.Framework.Extensions;

namespace VPLink2014
{
    public static class QuizzMapper
    {
        public static bool CheckIfUserAlreadyAnswered(string urlSite, string urlList, string userName, string quizzTitle)
        {
            bool found = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(urlSite))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList quizzList = web.GetList(urlList);
                            if (quizzList != null)
                            {
                                StringBuilder sbQuery = new StringBuilder();
                                sbQuery.AppendLine("<Where>");
                                sbQuery.AppendLine("<And>");
                                sbQuery.AppendFormat("   <Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq>", "Participant", userName);
                                sbQuery.AppendFormat("   <Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq>", "Titre_x0020_Quizz", quizzTitle);
                                sbQuery.AppendLine("</And>");
                                sbQuery.AppendLine("</Where>");

                                SPQuery query = new SPQuery() { Query = sbQuery.ToString() };
                                SPListItemCollection quizzUser = quizzList.GetSPListItemCollectionWithTextFields(new List<string>() { Fields.QuizzTitleField, Fields.ParticipantField }, query);

                                if (quizzUser != null)
                                {
                                    if (quizzUser.Count == 1)
                                    {
                                        found = true;
                                    }
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex) { }
            return found;
        }

        public static bool SaveQuizzAnswer(string urlSite, string urlList, string userName, string quizzTitle, string reponse)
        {
            bool answerSaved = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(urlSite))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList quizzList = web.GetList(urlList);
                            if (quizzList != null)
                            {
                                bool initAllowUnSafe = web.AllowUnsafeUpdates;
                                web.AllowUnsafeUpdates = true;
                                SPListItem newQuizzItem = quizzList.Items.Add();
                                newQuizzItem[Fields.ParticipantField] = userName;
                                newQuizzItem[Fields.QuizzTitleField] = quizzTitle;
                                newQuizzItem[Fields.ParticipantReponseField] = reponse;
                                newQuizzItem.Update();
                                web.AllowUnsafeUpdates = initAllowUnSafe;
                                answerSaved = true;
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
            }
            return answerSaved;
        }
    }
}
